package zhishu.iceyyy.com.kd_khd.er_fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import zhishu.iceyyy.com.kd_khd.R;
import zhishu.iceyyy.com.kd_khd.adapter.DemoAdapter;


/**
 * Created by HongJay on 2016/9/13.
 */
public class Recycle_Mainadapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private ArrayList<RecycleViewItemData> dataList;//数据集合
    List<String> ccList;
    Activity activity;


    public Recycle_Mainadapter(ArrayList<RecycleViewItemData> dataList, Activity activity) {
        this.dataList = dataList;
        this.activity = activity;
        ccList = new ArrayList<String>();
        ccList.add("111");
        ccList.add("222");
        ccList.add("333");
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //        头部
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_one, parent, false);
            OneViewHolder viewHolder = new OneViewHolder(view);
            return viewHolder;
        }
//       中部
        if (viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_two, parent, false);
            TwoViewHolder viewHolder = new TwoViewHolder(view);
            return viewHolder;
        }
//        列表
        if (viewType == 2) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_three, parent, false);
            ThreeViewHolder viewHolder = new ThreeViewHolder(view);
            return viewHolder;
        }


        return null;
    }


    @SuppressLint("WrongConstant")
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        //如果holder是EditViewHolder的实例
        if (holder instanceof OneViewHolder) {
            EditItem mEditItem = (EditItem) dataList.get(position).getT();

            Resources resources = activity.getResources();
            String[] stringArray = resources.getStringArray(R.array.url);
            initbanner(((OneViewHolder) holder).banner, stringArray);
        }

        if (holder instanceof TwoViewHolder) {

        }
        if (holder instanceof ThreeViewHolder) {
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
            linearLayoutManager.setAutoMeasureEnabled(true);
            Recycle_Listadapter demoAdapter = new Recycle_Listadapter();
            demoAdapter.adddata(ccList);
            ((ThreeViewHolder) holder).recycle.setLayoutManager(linearLayoutManager);
            ((ThreeViewHolder) holder).recycle.setAdapter(demoAdapter);

        }

    }

    @Override
    public int getItemViewType(int position) {
        if (0 == dataList.get(position).getDataType()) {
            return 0;//
        } else if (1 == dataList.get(position).getDataType()) {
            return 1;//
        } else if (2 == dataList.get(position).getDataType()) {
            return 2;//
        } else if (3 == dataList.get(position).getDataType()) {
            return 3;//
        } else {
            return 0;
        }
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    public class OneViewHolder extends RecyclerView.ViewHolder {
        public Banner banner;


        public OneViewHolder(View itemView) {
            super(itemView);
            banner = (Banner) itemView.findViewById(R.id.banner);

        }
    }


    public class TwoViewHolder extends RecyclerView.ViewHolder {


        public TwoViewHolder(View itemView) {
            super(itemView);
//
        }
    }

    public class ThreeViewHolder extends RecyclerView.ViewHolder {

        public RecyclerView recycle;
        public LinearLayout li;


        public ThreeViewHolder(View itemView) {
            super(itemView);

            recycle = (RecyclerView) itemView.findViewById(R.id.lv);
            li = (LinearLayout) itemView.findViewById(R.id.li);
        }
    }

//                public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
//                    this.onItemClickListener = onItemClickListener;
//                }
//    //回调接口
//    public interface OnItemClickListener {
//        /**
//         * \
//         *
//         * @param view item点视图
//         * @param data item的数据
//         */
//        public void OnItemClick(View view, RecycleViewItemData data);
//    }


    public class GlideImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            //Glide 加载图片简单用法
            Glide.with(context).load(path).into(imageView);


        }


    }

    private void initbanner(Banner banner, String[] imageUrls) {
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        List<String> resultList = new ArrayList<>(imageUrls.length);
        Collections.addAll(resultList, imageUrls);

        banner.setImages(resultList);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.DepthPage);
        //设置标题集合（当banner样式有显示title时）
//        banner.setBannerTitles(st2);
        //设置自动轮播，默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(2500);
        //设置指示器位置（当banner模式中有指示器时）
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();

    }

}
